package com.example.myapplicationktkt2

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplicationktkt2.activity.AdvancedUserActivity
import com.example.myapplicationktkt2.activity.UserActivity
import com.example.myapplicationktkt2.fragment.BottomNavigationActivity
import com.example.myapplicationktkt2.mini.MiniMainActivity2
import com.example.myapplicationktkt2.okhttp.OkHttpDemoActivity
import com.example.myapplicationktkt2.sqlite.SQLiteDemoActivity
import com.example.myapplicationktkt2.storage.DataStorageDemoActivity
import com.example.myapplicationktkt2.ui.*
import com.example.myapplicationktkt2.utils.PermissionHelper
import com.example.myapplicationktkt2.utils.nav.NavControls
import com.example.myapplicationktkt2.animation.AnimationDemoActivity
import com.example.myapplicationktkt2.audiovideo.AudioPlayerDemoActivity
import com.example.myapplicationktkt2.audiovideo.VideoPlayerDemoActivity
import com.example.myapplicationktkt2.canvas.CanvasStyleDemoActivity
import com.example.myapplicationktkt2.camera.CameraDemoActivity

/**
 * 我将按钮区域改为使用GridLayout，实现了左右两列布局，具体改进包括：
 * 使用GridLayout替代原来的LinearLayout来排列按钮
 * 设置columnCount="2"实现两列布局
 * 为每个按钮设置layout_columnWeight="1"，使它们平均分配每行的宽度
 * 使用alignmentMode="alignMargins"和useDefaultMargins="true"确保按钮对齐和间距合适
 * 3. 优化效果
 * 现在主界面的按钮将以两列形式展示，充分利用屏幕空间，提供更好的视觉效果和用户体验：
 * 每行显示两个按钮，总共22个按钮将排列成11行
 * 在大屏幕设备上避免了空间浪费
 * 在小屏幕设备上通过ScrollView仍然可以正常滚动查看所有内容
 * 保持了良好的可读性和足够的点击目标大小
 *
 */
class MainActivity : AppCompatActivity() {

    // 请求通知权限（保留原有逻辑）
    private val notificationPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
        if (isGranted) {
            // 权限已授予
        } else {
            // 权限被拒绝
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 使用 findViewById 和 setOnClickListener 的简化写法
        setupButton(R.id.btnLinearLayout, LinearLayoutDemoActivity::class.java)
        setupButton(R.id.btnRelativeLayout, RelativeLayoutDemoActivity::class.java)
        setupButton(R.id.btnFrameLayout, FrameLayoutDemoActivity::class.java)
        setupButton(R.id.btnBasicViews, BasicViewsDemoActivity::class.java)
        setupButton(R.id.btnWebView, WebViewActivity::class.java)
        setupButton(R.id.btnMiniApp, MiniMainActivity2::class.java)
        setupButton(R.id.btnCanvasStyles, CanvasStyleDemoActivity::class.java)
        setupButton(R.id.btnToastyDemo, ToastyDemoActivity::class.java)
        setupButton(R.id.btnComponentsDemo, DialogRecyclerViewActivity::class.java)
        setupButton(R.id.btnListViewDemo, ListViewItemActivity::class.java)
        setupButton(R.id.btnComplexListViewDemo, ComplexListViewDemoActivity::class.java)
        setupButton(R.id.btnWebViewNotification, WebViewActivity::class.java)
        setupButton(R.id.btnBottomNavigation, BottomNavigationActivity::class.java)
        setupButton(R.id.btnViewLogs, LogViewerActivity::class.java)
        setupButton(R.id.btnDataStorageDemo, DataStorageDemoActivity::class.java)
        setupButton(R.id.btnSQLiteDemo, SQLiteDemoActivity::class.java)
        setupButton(R.id.btnOkHttpDemo, OkHttpDemoActivity::class.java)
        setupButton(R.id.btnAnimationDemo, AnimationDemoActivity::class.java)
        
        // 添加音频播放演示按钮
        setupButton(R.id.btnAudioPlayerDemo, AudioPlayerDemoActivity::class.java)
        
        // 添加视频播放演示按钮
        setupButton(R.id.btnVideoPlayerDemo, VideoPlayerDemoActivity::class.java)
        
        // 添加相机使用演示按钮
        setupButton(R.id.btnCameraDemo, CameraDemoActivity::class.java)

        // 添加用户界面演示按钮
        setupButton(R.id.btnUserActivity, UserActivity::class.java)

        // 添加高级用户界面演示按钮
        setupButton(R.id.btnAdvancedUserActivity, AdvancedUserActivity::class.java)
        findViewById<Button>(R.id.btnOpenTaobao).setOnClickListener {
            NavControls.openTaobao(this)
        }

        // 请求通知权限
        PermissionHelper.checkAndRequestNotificationPermission(this) { permission ->
            notificationPermissionLauncher.launch(permission)
        }

    }

    private fun setupButton(buttonId: Int, activityClass: Class<*>) {
        findViewById<Button>(buttonId).setOnClickListener {
            startActivity(Intent(this, activityClass))
        }
    }
}